//
//  CellLabelWidget.m
//  UCiEnabler
//
//  Created by Delio Brignoli on 26/07/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "CellLabelWidget.h"

// cell identifier for this custom cell
NSString *kCellLabelWidget_ID = @"CellLabelWidget_ID";

@implementation CellLabelWidget

@synthesize view;
@synthesize nameLabel;

- (id)initWithFrame:(CGRect)aRect reuseIdentifier:(NSString *)identifier
{
	self = [super initWithFrame:aRect reuseIdentifier:identifier];
	if (self)
	{
		// turn off selection use
		self.selectionStyle = UITableViewCellSelectionStyleNone;

		nameLabel = [[UILabel alloc] initWithFrame:aRect];
		nameLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
		nameLabel.numberOfLines = 1;
		nameLabel.lineBreakMode = UILineBreakModeCharacterWrap;
		nameLabel.adjustsFontSizeToFitWidth = YES;
		nameLabel.minimumFontSize = 8.0;
		
		nameLabel.backgroundColor = [UIColor clearColor];
		nameLabel.opaque = NO;
		nameLabel.textColor = [UIColor blackColor];
		nameLabel.highlightedTextColor = [UIColor blackColor];
		nameLabel.font = [UIFont boldSystemFontOfSize:18];
		[self.contentView addSubview:nameLabel];
	}
	return self;
}

- (void)setView:(UIView *)inView
{
	[view removeFromSuperview];
	view = inView;
	//[self.view retain];

	if (inView != nil )
		[self.contentView addSubview:inView];

	[self layoutSubviews];
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	
	CGRect contentRect = [self.contentView bounds];
	CGRect viewBounds = CGRectZero;

	if (self.view)
		viewBounds = self.view.bounds;

	// In this example we will never be editing, but this illustrates the appropriate pattern
	CGRect frame = CGRectMake(contentRect.origin.x + kCellLeftOffset, (contentRect.size.height - kCellHeight)/2, contentRect.size.width - viewBounds.size.width - kCellLeftOffset, kCellHeight);
	nameLabel.frame = frame;
	//[nameLabel sizeToFit];
	
	frame = CGRectMake(contentRect.size.width - viewBounds.size.width - kCellLeftOffset,
							  round((contentRect.size.height - viewBounds.size.height) / 2.0),
							  viewBounds.size.width,
							  viewBounds.size.height);
	self.view.frame  = frame;
}

- (void)dealloc
{
	[nameLabel release];
    [view release];
    [super dealloc];
}

- (void)stopEditing
{
    [view resignFirstResponder];
}

#pragma mark -
#pragma mark <UITextFieldDelegate> Methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    BOOL beginEditing = YES;
    // Allow the cell delegate to override the decision to begin editing.
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellShouldBeginEditing:)])
	{
        beginEditing = [self.delegate cellShouldBeginEditing:self];
    }
    // Update internal state.
    if (beginEditing)
		self.isInlineEditing = YES;
    return beginEditing;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    // Notify the cell delegate that editing ended.
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellShouldBeginEditing:)])
	{
        [self.delegate cellDidEndEditing:self];
    }
    // Update internal state.
    self.isInlineEditing = NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self stopEditing];
    return YES;
}


@end
